package com.financegames.cashflow;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Random;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.AbsoluteLayout;
import android.widget.Button;
import android.widget.Toast;

import com.financegames.cards.BankruptCard;
import com.financegames.cards.BusinessCard;
import com.financegames.cards.Card;
import com.financegames.cards.ChanceCard;
import com.financegames.cards.ChildCard;
import com.financegames.cards.DivorceCard;
import com.financegames.cards.FiredCard;
import com.financegames.cards.MarketCard;
import com.financegames.cards.MarriageCard;
import com.financegames.cards.PayDayCard;
import com.financegames.cards.RealEstateCard;
import com.financegames.cards.RestCard;
import com.financegames.cards.SenatorCard;
import com.financegames.cards.SpendsCard;
import com.financegames.cards.StartCard;
import com.financegames.cards.TaxAuditCard;

public class NewGameActivity extends Activity {
    private static final int GameLenghtSteps = 10;
	private ArrayList<Card> fild = new ArrayList<Card>();
	
	/* field has to be a list of cards like this:
	 * 1. Start
	 * 2. Spends
	 * 3. Chance
	 * 4. Market
	 * 5. Child
	 * 6. Business
	 * 7. PayDay
	 * 8. Chance
	 * 9. Market
	 * 10.Business
	 * 11.Spends
	 * 12.Chance
	 * 13.Business
	 * 14.Spends
	 * 15.Market
	 * 16.Fired
	 * 17.Chance
	 * 18.Business
	 * 19.Spends
	 * 20.Business
	 * 21.Market
	 * 22.PayDay
	 * 
	 * 23.Chance
	 * 24.Spends
	 * 25.Business
	 * 26.Chance
	 * 27.Spends
	 * 28.Market
	 * 29.Senator
	 * 30.Spends
	 * 31.Marriage
	 * 32.RealEstate
	 * 33.Market
	 * 34.Spends
	 * 35.Business
	 * 36.PayDay
	 * 
	 * 37.Market
	 * 38.Business
	 * 39.Chance
	 * 40.Spends
	 * 41.Divorce
	 * 42.Market
	 * 43.Spends
	 * 44.Chance
	 * 45.Business
	 * 46.Chance
	 * 47.Market
	 * 48.Child
	 * 49.Spends
	 * 50.Business
	 * 51.PayDay
	 * 52.Market
	 * 53.Senator
	 * 54.Chance
	 * 55.Spends
	 * 56.Business
	 * 57.Spends
	 * 58.Market
	 * 59.Bankrupt
	 * 60.Spends
	 * 61.Chance
	 * 62.Business
	 * 63.Market
	 * 64.Chance
	 * 65.Spends
	 * 66.PayDay
	 * 
	 * 67.Chance
	 * 68.Market
	 * 69.Chance
	 * 70.Business
	 * 71.Spends
	 * 72.Senator
	 * 73.Market
	 * 74.Marriage
	 * 75.Chance
	 * 76.Rest
	 * 77.Business
	 * 78.Market
	 * 79.Chance
	 * 80.PayDay
	 * 
	 * 81.Market
	 * 82.Spends
	 * 83.Chance
	 * 84.Business
	 * 85.Tax Audit
	 * 86.Market
	 * 87.Spends
	 * 88.Chance
	 * 
	 * 
	 * 
	 * e
	 */
	
	private static final int HEIGHT = 14;
	private static final int WEIDTH = 30;
	private static final int START_OFFSET = 9;
	private static final int CELL_HEIGHT = 32; //depends on screen height
	private static final int CELL_WIDTH = 25;  //depends on screen width
	private static final int SCREEN_HEIGHT = 400; //for galaxy tab lanscape
	private static final int SCREEN_WIDTH = 750; //for galaxy tab lanscape
	
	// has to be cyclic
	private Iterator<Card> fieldIterator ;
	private Card currentCard;
	
	//current Card
	//Card currentCard;
	
	private ArrayList<Integer> moves = new ArrayList<Integer>();
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);
        
        chooseProfession();
        fillGameField();
        startGame();
        
        Button nextMoveButton = (Button)findViewById(R.id.next_button);
        nextMoveButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(isGameInProcess()) {
					throwingDice();
					movePiece();
					showCard();
				}else{
					Toast.makeText(NewGameActivity.this, "The Game is over!", Toast.LENGTH_SHORT).show();
				}
			}
		});
        
    }
    
	private void fillGameField() {
		Toast.makeText(this, "Preparing game field", Toast.LENGTH_SHORT).show();
		fild.add(new   StartCard());
		fild.add(new   SpendsCard());
		fild.add(new   ChanceCard());
		fild.add(new   MarketCard());
		fild.add(new   ChildCard());
		fild.add(new   BusinessCard());
		fild.add(new   PayDayCard());
		fild.add(new   ChanceCard());
		fild.add(new   MarketCard());
		fild.add(new   BusinessCard());
		fild.add(new   SpendsCard());
		fild.add(new   ChanceCard());
		fild.add(new   BusinessCard());
		fild.add(new   SpendsCard());
		fild.add(new   MarketCard());
		fild.add(new   FiredCard());
		fild.add(new   ChanceCard());
		fild.add(new   BusinessCard());
		fild.add(new   SpendsCard());
		fild.add(new   BusinessCard());
		fild.add(new   MarketCard());
		fild.add(new   PayDayCard());
		fild.add(new   ChanceCard());
		fild.add(new   SpendsCard());
		fild.add(new   BusinessCard());
		fild.add(new   ChanceCard());
		fild.add(new   SpendsCard());
		fild.add(new   MarketCard());
		fild.add(new   SenatorCard());
		fild.add(new   SpendsCard());
		fild.add(new   MarriageCard());
		fild.add(new   RealEstateCard());
		fild.add(new   MarketCard());
		fild.add(new   SpendsCard());
		fild.add(new   BusinessCard());
		fild.add(new   PayDayCard());
		fild.add(new   MarketCard());
		fild.add(new   BusinessCard());
		fild.add(new   ChanceCard());
		fild.add(new   SpendsCard());
		fild.add(new   DivorceCard());
		fild.add(new   MarketCard());
		fild.add(new   SpendsCard());
		fild.add(new   ChanceCard());
		fild.add(new   BusinessCard());
		fild.add(new   ChanceCard());
		fild.add(new   MarketCard());
		fild.add(new   ChildCard());
		fild.add(new   SpendsCard());
		fild.add(new   BusinessCard());
		fild.add(new   PayDayCard());
		fild.add(new   MarketCard());
		fild.add(new   SenatorCard());
		fild.add(new   ChanceCard());
		fild.add(new   SpendsCard());
		fild.add(new   BusinessCard());
		fild.add(new   SpendsCard());
		fild.add(new   MarketCard());
		fild.add(new   BankruptCard());
		fild.add(new   SpendsCard());
		fild.add(new   ChanceCard());
		fild.add(new   BusinessCard());
		fild.add(new   MarketCard());
		fild.add(new   ChanceCard());
		fild.add(new   SpendsCard());
		fild.add(new   PayDayCard());
		fild.add(new   ChanceCard());
		fild.add(new   MarketCard());
		fild.add(new   ChanceCard());
		fild.add(new   BusinessCard());
		fild.add(new   SpendsCard());
		fild.add(new   SenatorCard());
		fild.add(new   MarketCard());
		fild.add(new   MarriageCard());
		fild.add(new   ChanceCard());
		fild.add(new   RestCard());
		fild.add(new   BusinessCard());
		fild.add(new   MarketCard());
		fild.add(new   ChanceCard());
		fild.add(new   PayDayCard());
		fild.add(new   MarketCard());
		fild.add(new   SpendsCard());
		fild.add(new   ChanceCard());
		fild.add(new   BusinessCard());
		fild.add(new   TaxAuditCard());
		fild.add(new   MarketCard());
		fild.add(new   SpendsCard());
		fild.add(new   ChanceCard());
				
		fieldIterator = fild.iterator();
		
	}

	private void startGame() {
		Toast.makeText(this, "Starting new game", Toast.LENGTH_SHORT).show();
	}

	private void throwingDice() {
		moves.add(throwDice());
		Toast.makeText(this, "Throwing dice : " + lastMove(), Toast.LENGTH_SHORT).show();
	}

	private void showCard() {
		Toast.makeText(this, "CurrentCard is " + currentCard.getClass().toString(), Toast.LENGTH_SHORT).show();
	}

	private void movePiece() {
		//I really want to do it easy, like	moveToNextPlace();
		//fieldIterator.next(moves.getLast());
		for (int i=1;i<=lastMove();i++){
			currentCard = getNextField();
			redrawUserPiece(currentCard);
		}
	}

	private void redrawUserPiece(Card card) {
		int currentFieldPlace = calculateFieldPlace(card);
		drawUserPieceAt(currentFieldPlace);
	}

	private void drawUserPieceAt(int currentFieldPlace) {
		int x = calculateX(currentFieldPlace);
		int y = calculateY(currentFieldPlace);
		drawAt(currentFieldPlace,x,y);
	}

	private int calculateX(int currentFieldPlace) {
		currentFieldPlace += START_OFFSET;
		int x = 0;
		if (currentFieldPlace < WEIDTH){
			x = SCREEN_WIDTH - currentFieldPlace * CELL_WIDTH; 
		}else if(currentFieldPlace < WEIDTH + HEIGHT){
			x = 0;
		}else if(currentFieldPlace < 2 * WEIDTH + HEIGHT){
			x = (currentFieldPlace - WEIDTH - HEIGHT) * CELL_WIDTH;
		}else if(currentFieldPlace < 2 * WEIDTH + 2 * HEIGHT){
			x = SCREEN_WIDTH;
		}else{
			x = SCREEN_WIDTH - (currentFieldPlace - 88) * CELL_WIDTH;
		}
		return x;
	}

	private int calculateY(int currentFieldPlace) {
		currentFieldPlace += START_OFFSET;
		int y = 0;
		if (currentFieldPlace < WEIDTH){
			y = SCREEN_HEIGHT;
		}else if(currentFieldPlace < WEIDTH + HEIGHT){
			y = SCREEN_HEIGHT - (currentFieldPlace - WEIDTH) * CELL_HEIGHT; 
		}else if(currentFieldPlace < 2 * WEIDTH + HEIGHT){
			y = 0;
		}else if(currentFieldPlace < 2 * WEIDTH + 2 * HEIGHT){
			y = (currentFieldPlace - 2 * WEIDTH - HEIGHT) * CELL_HEIGHT;
		}else{
			y = SCREEN_HEIGHT;
		}
		return y;
	}

	private void drawAt(int index,int x, int y) {
		//create some UI element at (x,y)
		Toast.makeText(this, "# " + index + " X : " + x + " Y : " + y , Toast.LENGTH_SHORT).show();
		View image = findViewById(R.id.image);
		image.setLayoutParams(new AbsoluteLayout.LayoutParams(30, 30, x, y));
	}

	private int calculateFieldPlace(Card card) {
		return fild.indexOf(card);
	}

	private Card getNextField() {
		if (!fieldIterator.hasNext()) {
			fieldIterator = fild.iterator();
		}
		return fieldIterator.next();
	}

	private int lastMove() {
		return moves.get(moves.size() - 1);
	}

	private int throwDice() {
		//scale from 1..6
		return new Random().nextInt(6)+1;
	}

	private boolean isGameInProcess() {
		return moves.size()<GameLenghtSteps;
	}

	private void chooseProfession() {
		Toast.makeText(this, "Choosing profession", Toast.LENGTH_SHORT).show();
		
	}
}